home *** CD-ROM | disk | FTP | other *** search
- // uses universal headers
- //
- // snippet to demonstrate the use of the system 7 popup
- // control cdef in a program using modal dialogs
- //
- // Nick Thompson, 4/26/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
- /*
- Sponge ID:105648 New Question
-
- The content of your link dated: 31 May 1994 follows:
- ******************************************************************************
- ------------------------------------------------------------------------------
-
- I’m not sure who I should refer this to, but I believe I have found a bug in
- the new Dialog Manager routines SetDialogDefaultItem & GetStdFilterProc.
-
- What I am trying to do is create a default button that is initially grayed out
- and later enabled when the dialog box is filled in. What is happening is that
- my button is getting re-enabled (unless I step into the source debugger). My
- other non default button is getting grayed out as expected. I tried
- rearranging the order that things were done to get it to stay gray, but I
- couldn’t. I finally had to resort to using a filter proc and a global variable
- to tell me if I should gray the button after the initial call to
- GetStdFilterProc.
-
- ******************************************************************************
-
- */
-
- //updates 8/96: Prefix.h added as prefix file for 68K and PPC MC projects;
- //old routine names changed
-
- #include <Menus.h>
- #include <Processes.h>
- #include <Dialogs.h>
- #include <Fonts.h>
-
- pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
-
- const char kEnter = 0x03 ;
- const char kReturn = 0x0D ;
- const char kEscape = 0x1B ;
- const char kPeriod = '.' ;
-
- const char kThePopupMenu = 4 ; // popup is the fourth item in the dialog
- const char kUserTextArea = 5 ; // this is a rect for us to write a string
- // with the item chosen in it
-
-
- // Structure for the private data for a popup control.
- // This structure is documented on page 5-77
- // Inside Macintosh: Macintosh Toolbox Essentials
-
- typedef struct popupPrivateData {
- MenuHandle mHandle; // the popup menu handle
- short mID; // the popup menu ID
- // after these two public fields is the mPrivate private data,
- // which may be any old size and should not be messed with
- } popupPrivateData;
-
-
-
-
- void main(void) {
-
- DialogPtr thePopupDialog ;
- short itemHit ;
- popupPrivateData **myPopupPrivateDataPtr ;
-
- short iKind;
- Handle iHandle;
- Rect iRect;
-
- MenuHandle thePopupMenuHdl ;
- Str255 theItem ;
-
- OSErr theErr ;
-
- // initialize the toolbox
- InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
- TEInit(); InitDialogs((long)nil); InitCursor(); FlushEvents(everyEvent,0);
-
- thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
-
- SetPort( (GrafPtr)thePopupDialog ) ;
- theErr = SetDialogDefaultItem(thePopupDialog, ok) ;
-
- if( theErr != noErr )
- ExitToShell() ;
-
-
- do {
-
- ModalDialog ( NewModalFilterProc(OurFilter), &itemHit );
-
- if( itemHit == kThePopupMenu ) {
-
- // the user choose the popup. The item number selected will be the control value
- // we need to get the menuhandle associated with the control, it is in the private
- // control data field, as documented in Inside Macintosh: Toolbox page 5-77
-
- // get the control handle for the popup
- GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
-
- // extract from the control the menuhandle
- myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ;
- thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
-
- // get the string associated with the users selection
- GetMenuItemText ( thePopupMenuHdl, GetControlValue((ControlHandle)iHandle), theItem );
-
- // get the rect we are drawing in
- GetDialogItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
- SetDialogItemText ( iHandle, theItem );
-
- // this ensures that the update handler in the filter proc is called
- // as that is where we enable or disable the OK button
- InvalRect( &iRect ) ;
-
- // reset itemHit to something else or we'll continually redraw
- itemHit = 0 ;
-
- }
-
- } while( itemHit != ok ) ;
-
- DisposeDialog ( thePopupDialog );
-
- }
-
-
-
-
-
- pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
- {
-
- ModalFilterUPP theProc ;
- Boolean retVal ;
-
- static Boolean isDisabled = false ;
- OSErr theErr = noErr ;
-
- // stuff for getditems etc
- Str255 theItem ;
- short iKind;
- Handle iHandle;
- Rect iRect;
-
-
- // get the std filter proc
-
- theErr = GetStdFilterProc( &theProc ) ;
-
- if( theErr != noErr )
- ExitToShell() ;
-
- // try to call the standard filter, if it handles the event, we don't
- if( !(retVal = CallModalFilterProc(theProc, dlg, event, itemHit)) )
- {
- switch (event->what) {
-
- case nullEvent:
- break;
-
- case keyDown:
- case autoKey:
- retVal = false;
- break ;
-
- case updateEvt:
- // get the text item we are drawing in
- GetDialogItem ( dlg, kUserTextArea, &iKind, &iHandle, &iRect) ;
- GetDialogItemText ( iHandle, theItem );
- isDisabled = theItem[0] == '\0' ;
-
- // enable or disable the OK button depending on whether
- // we made a selection yet
- GetDialogItem( dlg, ok, &iKind, &iHandle, &iRect) ;
- if( isDisabled )
- HiliteControl( (ControlHandle)iHandle, 255 ) ;
- else
- HiliteControl( (ControlHandle)iHandle, 0 ) ;
-
- retVal = false;
- break ;
-
- default:
- retVal = false;
- break ;
- }
- }
-
- return retVal ;
- }
-
-
-